package org.example.ding.service;

import org.example.ding.entity.Dish;
import org.example.ding.entity.DishCategory;
import org.example.ding.repository.DishRepository;
import org.example.ding.repository.DishCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 数据初始化服务
 * 用于初始化系统的基础数据
 */
@Service
@Transactional
public class DataInitializationService {

    @Autowired
    private DishCategoryRepository dishCategoryRepository;

    @Autowired
    private DishRepository dishRepository;

    /**
     * 初始化所有基础数据
     */
    public void initializeAllData() {
        initializeCategories();
        initializeDishes();
    }

    /**
     * 检查菜品数据
     */
    public void checkDishData() {
        List<Dish> allDishes = dishRepository.findAll();
        System.out.println("总共有 " + allDishes.size() + " 个菜品");
        for (Dish dish : allDishes) {
            System.out.println("菜品: " + dish.getName() + ", 卡路里: " + dish.getCalories() +
                             ", 蛋白质: " + dish.getProtein() + ", 碳水: " + dish.getCarbohydrates() +
                             ", 脂肪: " + dish.getFat());
        }
    }

    /**
     * 初始化菜品分类
     */
    public void initializeCategories() {
        if (dishCategoryRepository.count() > 0) {
            return; // 已有数据，不重复初始化
        }

        List<CategoryData> categories = Arrays.asList(
            new CategoryData("主食", "米饭、面条、面包等主食类", DishCategory.MealType.LUNCH),
            new CategoryData("汤品", "各种汤类菜品", DishCategory.MealType.LUNCH),
            new CategoryData("素菜", "蔬菜类菜品", DishCategory.MealType.LUNCH),
            new CategoryData("荤菜", "肉类菜品", DishCategory.MealType.LUNCH),
            new CategoryData("海鲜", "海鲜类菜品", DishCategory.MealType.LUNCH),
            new CategoryData("早餐", "早餐专用菜品", DishCategory.MealType.BREAKFAST),
            new CategoryData("晚餐", "晚餐专用菜品", DishCategory.MealType.DINNER),
            new CategoryData("小食", "零食和小点心", DishCategory.MealType.SNACK),
            new CategoryData("饮品", "各种饮料", DishCategory.MealType.SNACK),
            new CategoryData("甜品", "甜点类食品", DishCategory.MealType.SNACK)
        );

        for (CategoryData categoryData : categories) {
            DishCategory category = new DishCategory();
            category.setName(categoryData.name);
            category.setDescription(categoryData.description);
            category.setMealType(categoryData.mealType);
            dishCategoryRepository.save(category);
        }
    }

    /**
     * 初始化菜品数据
     */
    public void initializeDishes() {
        if (dishRepository.count() > 0) {
            return; // 已有数据，不重复初始化
        }

        // 获取分类
        DishCategory mainFood = dishCategoryRepository.findByName("主食").orElse(null);
        DishCategory soup = dishCategoryRepository.findByName("汤品").orElse(null);
        DishCategory vegetable = dishCategoryRepository.findByName("素菜").orElse(null);
        DishCategory meat = dishCategoryRepository.findByName("荤菜").orElse(null);
        DishCategory seafood = dishCategoryRepository.findByName("海鲜").orElse(null);
        DishCategory breakfast = dishCategoryRepository.findByName("早餐").orElse(null);
        DishCategory snack = dishCategoryRepository.findByName("小食").orElse(null);

        // 初始化菜品数据
        List<DishData> dishes = Arrays.asList(
            // 主食类
            new DishData("白米饭", "优质大米蒸制的白米饭", mainFood, 
                new BigDecimal("130"), new BigDecimal("2.6"), new BigDecimal("28"), new BigDecimal("0.3"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.STEAMED, new BigDecimal("3.00")),
            
            new DishData("蛋炒饭", "鸡蛋炒制的香喷喷米饭", mainFood,
                new BigDecimal("200"), new BigDecimal("8.5"), new BigDecimal("30"), new BigDecimal("6.2"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.FRIED, new BigDecimal("12.00")),
            
            new DishData("牛肉面", "手工拉面配牛肉片", mainFood,
                new BigDecimal("280"), new BigDecimal("15.2"), new BigDecimal("45"), new BigDecimal("8.5"),
                Dish.SpiceLevel.MILD, Dish.CookingMethod.BOILED, new BigDecimal("18.00")),

            // 汤品类
            new DishData("紫菜蛋花汤", "清淡营养的紫菜蛋花汤", soup,
                new BigDecimal("45"), new BigDecimal("3.2"), new BigDecimal("2.1"), new BigDecimal("2.8"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED, new BigDecimal("8.00")),
            
            new DishData("冬瓜排骨汤", "清香的冬瓜排骨汤", soup,
                new BigDecimal("85"), new BigDecimal("8.5"), new BigDecimal("3.2"), new BigDecimal("4.1"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED, new BigDecimal("15.00")),

            // 素菜类
            new DishData("清炒小白菜", "新鲜小白菜清炒", vegetable,
                new BigDecimal("25"), new BigDecimal("1.8"), new BigDecimal("3.5"), new BigDecimal("0.8"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.FRIED, new BigDecimal("8.00")),
            
            new DishData("麻婆豆腐", "经典川菜麻婆豆腐", vegetable,
                new BigDecimal("120"), new BigDecimal("8.2"), new BigDecimal("6.5"), new BigDecimal("7.8"),
                Dish.SpiceLevel.HOT, Dish.CookingMethod.FRIED, new BigDecimal("12.00")),
            
            new DishData("蒜蓉西兰花", "蒜蓉炒制的西兰花", vegetable,
                new BigDecimal("35"), new BigDecimal("3.2"), new BigDecimal("4.8"), new BigDecimal("1.2"),
                Dish.SpiceLevel.MILD, Dish.CookingMethod.FRIED, new BigDecimal("10.00")),

            // 荤菜类
            new DishData("红烧肉", "经典红烧肉", meat,
                new BigDecimal("350"), new BigDecimal("25.8"), new BigDecimal("8.5"), new BigDecimal("25.2"),
                Dish.SpiceLevel.MILD, Dish.CookingMethod.FRIED, new BigDecimal("25.00")),
            
            new DishData("宫保鸡丁", "经典川菜宫保鸡丁", meat,
                new BigDecimal("220"), new BigDecimal("22.5"), new BigDecimal("12.8"), new BigDecimal("12.5"),
                Dish.SpiceLevel.MEDIUM, Dish.CookingMethod.FRIED, new BigDecimal("20.00")),
            
            new DishData("糖醋里脊", "酸甜可口的糖醋里脊", meat,
                new BigDecimal("280"), new BigDecimal("18.5"), new BigDecimal("25.8"), new BigDecimal("15.2"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.FRIED, new BigDecimal("22.00")),

            // 海鲜类
            new DishData("清蒸鲈鱼", "新鲜鲈鱼清蒸", seafood,
                new BigDecimal("120"), new BigDecimal("20.5"), new BigDecimal("0.8"), new BigDecimal("3.2"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.STEAMED, new BigDecimal("35.00")),
            
            new DishData("白灼虾", "新鲜白虾白灼", seafood,
                new BigDecimal("95"), new BigDecimal("18.8"), new BigDecimal("1.2"), new BigDecimal("1.8"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED, new BigDecimal("28.00")),

            // 早餐类
            new DishData("小笼包", "传统小笼包", breakfast,
                new BigDecimal("180"), new BigDecimal("8.5"), new BigDecimal("22.8"), new BigDecimal("6.8"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.STEAMED, new BigDecimal("15.00")),
            
            new DishData("豆浆", "新鲜豆浆", breakfast,
                new BigDecimal("35"), new BigDecimal("3.2"), new BigDecimal("2.8"), new BigDecimal("1.5"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.BOILED, new BigDecimal("3.00")),
            
            new DishData("煎蛋", "香煎鸡蛋", breakfast,
                new BigDecimal("150"), new BigDecimal("12.5"), new BigDecimal("0.8"), new BigDecimal("11.2"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.FRIED, new BigDecimal("5.00")),

            // 小食类
            new DishData("水果沙拉", "新鲜水果沙拉", snack,
                new BigDecimal("85"), new BigDecimal("1.2"), new BigDecimal("18.5"), new BigDecimal("2.8"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.RAW, new BigDecimal("12.00")),
            
            new DishData("酸奶", "原味酸奶", snack,
                new BigDecimal("65"), new BigDecimal("3.8"), new BigDecimal("8.5"), new BigDecimal("2.2"),
                Dish.SpiceLevel.NONE, Dish.CookingMethod.RAW, new BigDecimal("6.00"))
        );

        for (DishData dishData : dishes) {
            if (dishData.category != null) {
                Dish dish = new Dish();
                dish.setName(dishData.name);
                dish.setDescription(dishData.description);
                dish.setCategory(dishData.category);
                dish.setCalories(dishData.calories);
                dish.setProtein(dishData.protein);
                dish.setCarbohydrates(dishData.carbohydrates);
                dish.setFat(dishData.fat);
                dish.setFiber(BigDecimal.ZERO);
                dish.setSodium(BigDecimal.ZERO);
                dish.setSpiceLevel(dishData.spiceLevel);
                dish.setCookingMethod(dishData.cookingMethod);
                dish.setPrice(dishData.price);
                dish.setPopularityScore(BigDecimal.ZERO);
                dish.setOrderCount(0);
                dish.setIsAvailable(true);
                
                dishRepository.save(dish);
            }
        }
    }

    /**
     * 分类数据内部类
     */
    private static class CategoryData {
        final String name;
        final String description;
        final DishCategory.MealType mealType;

        CategoryData(String name, String description, DishCategory.MealType mealType) {
            this.name = name;
            this.description = description;
            this.mealType = mealType;
        }
    }

    /**
     * 菜品数据内部类
     */
    private static class DishData {
        final String name;
        final String description;
        final DishCategory category;
        final BigDecimal calories;
        final BigDecimal protein;
        final BigDecimal carbohydrates;
        final BigDecimal fat;
        final Dish.SpiceLevel spiceLevel;
        final Dish.CookingMethod cookingMethod;
        final BigDecimal price;

        DishData(String name, String description, DishCategory category,
                BigDecimal calories, BigDecimal protein, BigDecimal carbohydrates, BigDecimal fat,
                Dish.SpiceLevel spiceLevel, Dish.CookingMethod cookingMethod, BigDecimal price) {
            this.name = name;
            this.description = description;
            this.category = category;
            this.calories = calories;
            this.protein = protein;
            this.carbohydrates = carbohydrates;
            this.fat = fat;
            this.spiceLevel = spiceLevel;
            this.cookingMethod = cookingMethod;
            this.price = price;
        }
    }
}
